<?php

require('event-log.php');

// Use the PHP syslog values
/*
$LOG_LEVELS = array(
	 'LOG_EMERG' => LOG_EMERG
	,'LOG_ALERT' => LOG_ALERT
	,'LOG_CRIT' => LOG_CRIT
	,'LOG_ERR' => LOG_ERR
	,'LOG_WARNING' => LOG_WARNING
	,'LOG_NOTICE' => LOG_NOTICE
	,'LOG_INFO' => LOG_INFO
	,'LOG_DEBUG' => LOG_DEBUG
);
*/

$LOG_LEVELS = array(
	 'LOG_EMERG' 		=> EL_EMERG
	,'LOG_ALERT' 		=> EL_ALERT
	,'LOG_CRIT' 		=> EL_CRIT
	,'LOG_ERR' 			=> EL_ERR
	,'LOG_WARNING' 	=> EL_WARNING
	,'LOG_NOTICE' 		=> EL_NOTICE
	,'LOG_INFO'		 	=> EL_INFO
	,'LOG_DEBUG' 		=> EL_DEBUG
);

/*
$log = new EventLog('test');
for($i=0; $i < 150; $i++) {
	$level = $LOG_LEVELS[array_rand($LOG_LEVELS)];
	$log->write('this is a random log level '.md5(microtime().rand(1000,10000)), $level);
//	sleep(rand(1,5));
}
*/

if(!isset($_GET['levels']))
	$_GET['levels'] = array_keys($LOG_LEVELS);
if(!isset($_GET['age']))
	$_GET['age'] = '600';
if(!isset($_GET['rsort']))
	$_GET['rsort'] = 'true';


$FLAGS = 0;
$unfiltered_levels = array();
foreach($_GET['levels'] as $v) {
	$unfiltered_levels[] = $LOG_LEVELS[$v];
	$FLAGS = $FLAGS|$LOG_LEVELS[$v];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Event Log - Viewer</title>
</head>

<body>
<?php
function level($value) {
	$selected = is_array($_GET['levels']) && in_array($value, $_GET['levels']) ? ' checked="checked"' : '';
	$id = md5($value.microtime());
	echo '<input type="checkbox" id="chk-'.$id.'" name="levels[]" value="'.$value.'"'.$selected.' /> <label for="chk-'.$id.'">'.$value.'</label><br />';
}

function output_search() {
	global $LOG_LEVELS;
?>
<form action="event-log-view.php" method="get">
   <strong>Filter</strong><br />
<?php
	foreach($LOG_LEVELS as $k => $v)
		level($k);
?>
<br />
   <strong>Timeframe</strong><br />
   <select name="age">
   	<option></option>
   	<option value="120"<?php if($_GET['age']=='120') echo ' selected="selected"'; ?>>2 minutes</option>
   	<option value="600"<?php if($_GET['age']=='600') echo ' selected="selected"'; ?>>10 minutes</option>
   	<option value="3600"<?php if($_GET['age']=='3600') echo ' selected="selected"'; ?>>1 hour</option>
   	<option value="86400"<?php if($_GET['age']=='86400') echo ' selected="selected"'; ?>>24 hours</option>
   	<option value="259200"<?php if($_GET['age']=='259200') echo ' selected="selected"'; ?>>3 days</option>
   	<option value="604800"<?php if($_GET['age']=='604800') echo ' selected="selected"'; ?>>1 week</option>
   	<option value="1209600"<?php if($_GET['age']=='1209600') echo ' selected="selected"'; ?>>2 weeks</option>
   	<option value="2678400"<?php if($_GET['age']=='2678400') echo ' selected="selected"'; ?>>1 month</option>
   </select>

<br /><br />

   <strong>Timeframe</strong><br />
	<input type="checkbox" id="chk-rsort" name="rsort" value="true"<?php if(isset($_GET['rsort'])) echo ' checked="checked"'; ?> /> <label for="chk-rsort">Show newest entries first</label><br />
<br />
  	<input type="submit" value="View" /> <a href="?">&raquo; Clear</a>
</form>
<?php
}


?>
<div id="search" style="/*display:none; */background:#f0f0f0; padding:10px;">
<?php
	output_search();
?>
</div> <a href="#" onclick="document.getElementById('search').style.display='block'; this.style.display='none'; ">&raquo; Show Search</a>

	<table cellpadding="3" cellspacing="0" border="0" width="100%">
      <tr>
         <th width="200" align="left">Date</th>
         <th width="100">Level</th>
         <th align="left">Text</th>
      </tr>
<?php
	$paths = array();
	
	$handle = fopen(EL_LOG_PATH.'/'.EL_LOG_FILE, 'r') || die('Error: Could not open log file.');
	while (false !== ($data = fgetcsv($handle, 8192, "\t"))) {
		if(isset($_GET['age']) && strtotime($data[0]) < (time()-$_GET['age'])) continue;
		if(!($data[1]&$FLAGS)) continue;

		/*
		$this->get_date(EL_LINE_DATE)
		,$priority
		,$this->filter_text($path)
		,$this->filter_text($str)
		,$this->filter_text($description)
		,$this->filter_text($code)
		*/	
		
?>
			<tr>
            <td align="left"><?php echo $data[0]; ?></td>
            <td align="center"><?php echo $data[1]; ?></td>
            <td>
					<?php 
					if($data[5]!='') 
						echo '['.$data[5].'] '; 
					?>
					<?php 
					echo htmlentities(EventLog::unfilter_text($data[3])); 
					?>
               <div class="description" style="display:none;">
						<?php 
                  echo htmlentities(EventLog::unfilter_text($data[4])); 
                  ?>
               </div>
				</td>
         </tr>
<?php

		$paths[$data[2]] = isset($paths[$data[2]]) ? $paths[$data[2]]+1 : 1;
			
//		$row++;
//		if($row>10) break;
	}
	fclose($handle);
	
//	echo '<pre>';
//	print_r($paths);

//		$line = str_replace('{{log-sep}}', EL_LOG_SEPARATOR, $line);
?>
	</table>	

</body>
</html>
